package com.groupfifteen.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.groupfifteen.common.R;
import com.groupfifteen.entity.SysFillQuestionEntity;
import com.groupfifteen.entity.SysUserEntity;
import com.groupfifteen.service.SysFillQuestionService;
import com.groupfifteen.util.ApiResultHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("sysfillquestion")
public class SysFillQuestionController {
    @Autowired
    private final SysFillQuestionService sysFillQuestionService;

//
    @PostMapping("/query")
    public IPage<SysFillQuestionEntity> querySysFillQuestions(@RequestBody SysFillQuestionEntity sysFillQuestionEntity,
                                                              @RequestParam(defaultValue = "1") int pageNo,
                                                              @RequestParam(defaultValue = "10") int pageSize) {
        return sysFillQuestionService.QuerySysFillQuestions(sysFillQuestionEntity, pageNo, pageSize);
    }
    @PostMapping("addfillquestion")
    public String AddFillQuestion(@RequestBody SysFillQuestionEntity sysFillQuestionEntity) {
        Integer result = sysFillQuestionService.AddFillQuestion(sysFillQuestionEntity);
        if (result > 0) {
            return "success";
        } else {
            return "fail";
        }
    }
    //
    @DeleteMapping("/deletefillquestion/{question_id}")
    public String DeleteFillQuestion(@PathVariable("question_id") Integer question_id) {
        Integer result = sysFillQuestionService.DeleteFillQuestion(question_id);
        if (result > 0) {
            return "success";
        } else {
            return "fail";
        }
    }

    @GetMapping("/getfillquestion/{question_id}")
    public SysFillQuestionEntity GetFillQuestion(@PathVariable("question_id") Integer question_id) {
        SysFillQuestionEntity result = sysFillQuestionService.GetFillQuestion(question_id);
        if (result != null) {
            return result;
        } else {
            return null;
        }
    }

    @PutMapping("/updatefillquestion/{question_id}")
    public String UpdateFillQuestion(@PathVariable("question_id")Integer question_id,@RequestBody SysFillQuestionEntity sysFillQuestionEntity) {
        sysFillQuestionEntity.setQuestionId(question_id);
        Integer result = sysFillQuestionService.UpdateFillQuestion(sysFillQuestionEntity);
        if (result > 0) {
            return "success";
        } else {
            return "fail";
        }
    }


//    /**
//     * 添加填空题
//     *
//     * @param fillQuestion 填空题内容
//     * @return 结果
//     */
//    @PostMapping("/fillQuestion")
//    public R add(@RequestBody SysFillQuestionEntity fillQuestion) {
//        int res = fillQuestionService.add(fillQuestion);
//        if (res != 0) {
//            return ApiResultHandler.buildApiResult(200, "添加成功", res);
//        }
//        return ApiResultHandler.buildApiResult(400, "添加失败", res);
//    }

    /**
     * 查询最后一条填空题
     *
     * @return 最后一条填空题
     */
    @GetMapping("/fillQuestionId")
    public R<SysFillQuestionEntity> findOnlyQuestionId() {
        SysFillQuestionEntity res = sysFillQuestionService.findOnlyQuestionId();
        return ApiResultHandler.buildApiResult(200, "查询成功", res);
    }

//    /**
//     * 编辑填空题
//     *
//     * @param fillQuestion 填空题内容
//     * @return 结果
//     */
//    @PostMapping("/editFillQuestion")
//    public R edit(@RequestBody SysFillQuestionEntity fillQuestion) {
//        int res = fillQuestionService.edit(fillQuestion);
//        if (res != 0) {
//            return ApiResultHandler.buildApiResult(200, "修改成功", res);
//        }
//        return ApiResultHandler.buildApiResult(400, "修改失败", res);
//    }
}